from django.contrib import admin
from .models import Goods
from .models import GoodsImage
from .models import AreaWareHouse
from .models import PriceVipCoe
from .models import SupplyOrder
from .models import WomaiOrder
from .models import WomaiChildOrder
from .models import GoodsType
from .models import YF
from .models import Company
from .models import StartTime
from .models import Category
from .models import Inventory
from .models import ReceiverPlace
from .models import IndexPre
from .models import Banner
from .models import GoodsAttribute
from .models import GoodsAttributeValue
from .models import GoodsPrice
from .models import BasePopularImg
from .models import PopularQrCode
from .models import RefundRecord
from .models import TaiProjectLine
from .models import BackDoorTai


class BackDoorTaiAdmin(admin.ModelAdmin):
    list_display = ("id", "channel_name", "mobile")

    def channel_name(self, model):
        return model.channel.name

    channel_name.short_description = "产品线名称"


class TaiProjectLineAdmin(admin.ModelAdmin):
    list_display = ("id", "name",)


class RefundRecordAdmin(admin.ModelAdmin):
    list_display = ("id", "refund_type_desc", "out_refund_no")

    def refund_type_desc(self, model):
        return model.get_refund_type_display()


class BannerAdmin(admin.ModelAdmin):
    list_display = ("id", "url", "order", "type_desc")

    def type_desc(self, model):
        return model.get_type_display()


class ReceiverPlaceAdmin(admin.ModelAdmin):
    list_display = ("user_name",)

    def user_name(self, model):
        return model.user.name


class IndexPreAdmin(admin.ModelAdmin):
    list_display = ("desc", "permission", "order")


class InventoryAdmin(admin.ModelAdmin):
    list_display = ("id", "num", "area_name", "goods_name", "area_code")

    def area_name(self, model):
        return model.province.name

    def area_code(self, model):
        return model.province.area_code

    def goods_name(self, model):
        return model.goods.goodsname

    goods_name.short_description = "商品名称"
    area_name.short_description = "区域名称"
    area_code.short_description = "省份编码"


class CategoryAdmin(admin.ModelAdmin):
    list_display = ("id", "name")


class WomaiChildOrderInline(admin.TabularInline):
    model = WomaiChildOrder
    max_num = 20
    extra = 1  # 用于添加外键关系模型的空行
    verbose_name = '我买网子订单'  # 外键关系模型列表最下面的添加另外一个***,所用的名字
    show_change_link = True  # 显示修改连接,这个很有用啊!!!!
    can_delete = True  # 是否显示删除框


class WomaiOrderAdmin(admin.ModelAdmin):
    list_display = ("id", "tid", "ordercode", "check_status", "order_state", "submitstate", "update_time")
    inlines = [WomaiChildOrderInline, ]


class WomaiChildOrderAdmin(admin.ModelAdmin):
    list_display = ("id", "is_promotion", "goods_name", "oid", "num", "price", "total_fee", "state")

    def goods_name(self, model):
        return model.goods.goodsname

    goods_name.short_description = "商品名称"


class WomaiOrderInline(admin.TabularInline):
    model = WomaiOrder
    max_num = 20
    extra = 1  # 用于添加外键关系模型的空行
    verbose_name = '我买网父订单'  # 外键关系模型列表最下面的添加另外一个***,所用的名字
    show_change_link = True  # 显示修改连接,这个很有用啊!!!!
    can_delete = True  # 是否显示删除框


class SupplyOrderAdmin(admin.ModelAdmin):
    list_display = ("id", "total_fee", "payment", "created_time", "modified")
    inlines = [WomaiOrderInline, ]

    def salesman_desc(self, model):
        return model.salesman.name

    salesman_desc.short_description = "用户"


class AreaWareHouseAdmin(admin.ModelAdmin):
    list_display = ("province_desc", "p_code")

    def province_desc(self, model):
        return model.province.name

    province_desc.short_description = "省份名称"


class GoodsImageInline(admin.TabularInline):
    model = GoodsImage
    max_num = 30
    extra = 1  # 用于添加外键关系模型的空行
    verbose_name = '商品图片'  # 外键关系模型列表最下面的添加另外一个***,所用的名字
    show_change_link = True  # 显示修改连接,这个很有用啊!!!!
    can_delete = True  # 是否显示删除框


class GoodsAdmin(admin.ModelAdmin):
    inlines = [GoodsImageInline, ]
    # readonly_fields = ("sales_price_isvip",)
    list_display = (
        "id", "goodsname", "index_pre_desc", "is_promotion", "is_vip_bag_desc", "company_desc", "system_category_desc",
        "post_fee_desc", "can_sale", "ordering", "cost", "sales_price")
    search_fields = ("id", "goodsname",)

    def post_fee_desc(self, model):
        if model.post_fee == 0:
            return "包邮商品"
        else:
            return model.post_fee

    def index_pre_desc(self, model):
        if model.index_pre:
            return model.index_pre.desc
        else:
            return "不在任何专区中"

    def is_promotion(self, model):
        if model.promotion > 0:
            return "促销商品"
        else:
            return "非促销商品"

    def is_vip_bag_desc(self, model):
        if model.is_vip_bag:
            return "会员礼包"
        else:
            return "非会员礼包"

    def company_desc(self, model):
        if model.company:
            return model.company.name
        else:
            return "未添加"

    def system_category_desc(self, model):
        if model.system_category:
            return model.system_category.name
        else:
            return "暂无分类"

    index_pre_desc.short_description = "首页分类"
    is_promotion.short_description = "是否是促销商品"
    is_vip_bag_desc.short_description = "是否是会员礼包"
    company_desc.short_description = "公司"
    system_category_desc.short_description = "系统分类"
    post_fee_desc.short_description = "运费"


class PriceVipCoeAdmin(admin.ModelAdmin):
    list_display = ("coe", "update_time",)


class YFAdmin(admin.ModelAdmin):
    list_display = ("goods_type_desc", "sz", "sz_price", "xz_price", "province_name")

    def goods_type_desc(self, model):
        return model.goods_type.name

        # def province_desc(self, model):
        #     return model.province.name


class GoodsTypeAdmin(admin.ModelAdmin):
    list_display = ("id", "name", "goods_type")


class CompanyAdmin(admin.ModelAdmin):
    list_display = ("name", "sign", "tel")


class StartTimeGoodsInline(admin.TabularInline):
    model = Goods
    max_num = 20
    extra = 1  # 用于添加外键关系模型的空行
    verbose_name = '商品'  # 外键关系模型列表最下面的添加另外一个***,所用的名字
    show_change_link = True  # 显示修改连接


class StartTimeAdmin(admin.ModelAdmin):
    # inlines = [StartTimeGoodsInline, ]
    list_display = ("id", "start_time")


class GoodsAttributeValueInline(admin.TabularInline):
    model = GoodsAttributeValue
    max_num = 20
    extra = 1  # 用于添加外键关系模型的空行
    verbose_name = '属性值'  # 外键关系模型列表最下面的添加另外一个***,所用的名字
    show_change_link = True  # 显示修改连接,这个很有用啊!!!!
    can_delete = True  # 是否显示删除框


class GoodsPriceAdmin(admin.ModelAdmin):
    list_display = ("goods_name", "sales_price", "other_shop_sales_price", "sales_price_isvip", "cost")

    def goods_name(self, model):
        return model.goods.goodsname

    goods_name.short_description = "商品名称"


# 属性类别
class GoodsAttributeAdmin(admin.ModelAdmin):
    list_display = ("attr",)


# 商品属性值
class GoodsAttributeValueAdmin(admin.ModelAdmin):
    list_display = ("value", "attribute_desc", "price_desc", "goods_name")
    search_fields = ("price__goods__goodsname",)

    def attribute_desc(self, model):
        return model.attribute.attr

    def price_desc(self, model):
        return round(model.price.sales_price / 100.0, 2)

    def goods_name(self, model):
        return model.price.goods.goodsname

    attribute_desc.short_description = "属性描述"
    price_desc.short_description = "销售价"
    goods_name.short_description = "商品名称"


class PopularQrCodeAdmin(admin.ModelAdmin):
    list_display = ("user_name", "url", "img_ordering")

    def user_name(self, model):
        return model.user.name

    def img_ordering(self, model):
        return model.img.ordering

    user_name.short_description = "用户名"
    img_ordering.short_description = "排序"


admin.site.register(Goods, GoodsAdmin)
admin.site.register(AreaWareHouse, AreaWareHouseAdmin)
admin.site.register(PriceVipCoe, PriceVipCoeAdmin)
admin.site.register(SupplyOrder, SupplyOrderAdmin)
admin.site.register(WomaiOrder, WomaiOrderAdmin)
admin.site.register(WomaiChildOrder, WomaiChildOrderAdmin)
admin.site.register(YF, YFAdmin)
admin.site.register(GoodsType, GoodsTypeAdmin)
admin.site.register(Company, CompanyAdmin)
admin.site.register(StartTime, StartTimeAdmin)
admin.site.register(Category, CategoryAdmin)
admin.site.register(Inventory, InventoryAdmin)
admin.site.register(ReceiverPlace, ReceiverPlaceAdmin)
admin.site.register(IndexPre, IndexPreAdmin)
admin.site.register(Banner, BannerAdmin)
admin.site.register(GoodsPrice, GoodsPriceAdmin)
admin.site.register(GoodsAttribute, GoodsAttributeAdmin)
admin.site.register(GoodsAttributeValue, GoodsAttributeValueAdmin)
admin.site.register(BasePopularImg)
admin.site.register(PopularQrCode, PopularQrCodeAdmin)
admin.site.register(RefundRecord, RefundRecordAdmin)
admin.site.register(BackDoorTai, BackDoorTaiAdmin)
admin.site.register(TaiProjectLine, TaiProjectLineAdmin)
